﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_News_News : System.Web.UI.Page
{
    protected string pDel_mk = "", pUpd_mk="";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);
        if (clsAuth.IsDeleteAuth() == true)
            pDel_mk = "Y";
        if (clsAuth.IsUpdateAuth() == true)
            pUpd_mk = "Y";

        if (!IsPostBack)
        {
            txtTenTheLoai.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; "); 

            if (clsAuth.IsAddAuth() == false)
                btnAdd_Catalogue.Visible = false;
            BindTopic();
            BinData();
        }
    }
    protected void btnAdd_Catalogue_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddNews.aspx?CodeID=" + Request.QueryString["CodeID"]);
    }

    private void BindTopic()
    {
        ListItem item = new ListItem();
        item.Text = "Tin tức";
        item.Value = "N";
        ddlChuDe.Items.Add(item);
        item = new ListItem();
        item.Text = "Khuyến mãi";
        item.Value = "S";
        ddlChuDe.Items.Add(item);

        item = new ListItem();
        item.Text = "Giới thiệu HD";
        item.Value = "I";
        ddlChuDe.Items.Add(item);

        ddlChuDe.SelectedIndex = -1;

        if (Request.QueryString["KindNo"] != "" && Request.QueryString["KindNo"] != null)
            ddlChuDe.Items.FindByValue(Request.QueryString["KindNo"]).Selected = true;
    }

    private void BinData()
    {
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("NewsID", "0");
        msg.CreateFirstNode("Title", txtTenTheLoai.Text.Trim());
        msg.CreateFirstNode("KindNo", ddlChuDe.SelectedValue);
        msg.CreateFirstNode("Status", "N");
        msg.CreateFirstNode("ViewCount", "20");
        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_NewsByCondition(msg.GetXmlStr);
        grv_Master.DataSource = ds;
        grv_Master.DataBind();

    }
    protected void grv_Master_RowCreated(object sender, GridViewRowEventArgs e)
    {
        /*for (int i = 0; i < grv_Master.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.WebControl myControl = grv_Master.Rows[i].Cells[1];
            myControl.Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
        }*/


    }
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].ColumnSpan = 2;
            e.Row.Cells.RemoveAt(1);
        }
        else if (e.Row.RowType == DataControlRowType.DataRow)
        {
            /* Label lblKindNo = (Label)e.Row.FindControl("lblKindNo");
             if (lblKindNo.Text == "N")
                 lblKindNo.Text = "Tin tức";
             else if (lblKindNo.Text == "S")
                 lblKindNo.Text = "Khuyến mãi";
             else if (lblKindNo.Text == "I")
                 lblKindNo.Text = "Giới thiệu HD";
             else
                 lblKindNo.Text = "Download";
      */


            e.Row.Cells[1].Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
            //Neu UserID=-1 thi hien thi Administrator
            if (DataBinder.Eval(e.Row.DataItem, "UpdID").ToString() == "-1")
            {
                Label lblUserNm = (Label)e.Row.FindControl("lblUserNm");
                lblUserNm.Text = "Administrator";
            }
            //Truong hop xoa
            if (pDel_mk != "Y")
            {
                e.Row.Cells[1].Text = "";
                e.Row.Cells[1].Attributes.Add("onclick", "javascript:return false;");
            }
            //Truong hop Update
            if (pUpd_mk != "Y")
            {
                e.Row.Cells[0].Text = "";
                e.Row.Cells[0].Attributes.Add("onclick", "javascript:return false;");
            }




            e.Row.Cells[4].Text = FormatDate(e.Row.Cells[4].Text);

            e.Row.Attributes.Add("onmouseover", "this.className='cursorOverGrid'");
            if (e.Row.RowIndex % 2 == 0)
                e.Row.Attributes.Add("onmouseout", "this.className='gvRowStyle'");
            else
                e.Row.Attributes.Add("onmouseout", "this.className='gvAlternatingRowStyle'");
        }


    }
    private string FormatDate(string sDate)
    {
        string sReturn = "";
        try
        {
            sReturn = sDate.ToString().Substring(6, 2) + "/" + sDate.ToString().Substring(4, 2) + "/" + sDate.ToString().Substring(0, 4);
        }
        catch (Exception ex) { }

        return sReturn;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BinData();
    }

    protected void grv_Master_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("NewsID", grv_Master.DataKeys[e.RowIndex].Value.ToString());
        cls.Del_News(msg.GetXmlStr.ToString());
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        BinData();
    }
    protected void btnSearch_Click1(object sender, EventArgs e)
    {
        BinData();
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinData();
    }
    protected void grv_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Master.PageIndex = e.NewPageIndex;
        BinData();
    }
}
